import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
option.add_experimental_option('excludeSwitches',['enable-automation'])
option.add_experimental_option('useAutomationExtension',False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{'source':'Object.defineProperty(navigator,"webdriver",{get:()=>undefind})'})
driver.get("https://www.baidu.com")
# driver.add_cookie({'name':'userName','value':'15207535664'})# 添加用户名cookie
# driver.add_cookie({'name':'password','value':'@jiatenghui.'})# 添加密码cookie
# dr.get("http://qzx:8080/Test1.4/")# 再次打开同一页面会自动登录

# driver.refresh()# 刷新页面会自动登录
# send_keys 在对象上模拟按键输入
driver.find_element(By.ID,"kw").send_keys("魔女之旅")
# 点击对象 click()
driver.find_element(By.ID,"su").click()
time.sleep(4)
# 清除百度输入框中的内容
driver.find_element_by_id("kw").clear()
time.sleep(4)

driver.find_element_by_css_selector("#kw").send_keys("零之使魔")

# 提交表单 submit
# driver.find_element(By.CSS_SELECTOR,"#su").click()
driver.find_element(By.CSS_SELECTOR,"#su").submit()

# 添加等待
driver.implicitly_wait(10)
# time.sleep(2)
# text 用户获取元素的文本信息
data = driver.find_element(By.XPATH,"//*[@id='1']/div/div/h3/a").text
print(data)

time.sleep(4)
driver.quit()